<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */

namespace HiCommon\Repository;

use HiCommon\Model\CheckpointMakeDate;

class CheckpointMakeDateRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\CheckpointMakeDate();
    }

    /**
     * 检测机构-近期预约日期列表
     *
     * @param int $n_shop_id
     * @param int $take_num
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function appointment_days_nearly($n_shop_id = 0,$take_num = 10){
        $arr_where = ['checkpoint_id'=>$n_shop_id];
        $result = self::model_child()
          ->where($arr_where)
            ->where('day','>=',date('Y-m-d',time()))
            ->orderBy('day','asc')
            ->take($take_num)
            ->select('id','checkpoint_id','day','am_time','am_num','am_order','pm_time','pm_num','pm_order')
        ->get();
        return $result;
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($option = [])
    {
        $model = new CheckpointMakeDate();

        if (isset($option['checkpoint_id']) && $option['checkpoint_id']) {
            $model = $model->where('checkpoint_id', $option['checkpoint_id']);
        }

        if (isset($option['start']) && $option['start'] && isset($option['end']) && $option['end']) {
            $model = $model->where('day', '>=', $option['start']);
            $model = $model->where('day', '<=', $option['end']);
        } else {
            $model = $model->where('day', '>=', $option['start']);
        }
        $limit = $option['limit'] ?? 20;
        return $model->orderBy('day', 'asc')->paginate($limit);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_common($arr_option = [])
    {
        return CheckpointMakeDate::commonpage($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_effective_list_common($arr_option = [])
    {
        return CheckpointMakeDate::where('day','>=',date('Y-m-d',time()))
          ->apipage($arr_option);
    }

    /**
     * 获取所有数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_data($arr_option = [])
    {
        return CheckpointMakeDate::commonget($arr_option);
    }

    /**
     * 修改或创建一条数据
     */
    public function edit_or_create(array $arr_where, array $arr_input)
    {
        return CheckpointMakeDate::updateorcreate($arr_where, $arr_input);
    }

    /**
     * 添加多条数据
     *
     */
    public function add_more($more)
    {
        return CheckpointMakeDate::insert($more);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(array $where = [])
    {
        return CheckpointMakeDate::where($where)->first();
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return CheckpointMakeDate::where('id', $n_id)->commondelete(true);
    }

    //查询已有数据
    public function get_all(array $where = [])
    {
        return CheckpointMakeDate::where($where)->get(['day'])->toarray();
    }

    public function get_all_between(array $where = [] ,$between)
    {
        return CheckpointMakeDate::where($where)->whereBetween('day', $between)->get(['day'])->toarray();
    }
    /**
     * 删除
     */
    public function del_this_all(int $che_id)
    {
        return CheckpointMakeDate::where('checkpoint_id', $che_id)->commondelete();
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return CheckpointMakeDate::where('id', $arr_input['id'])->update($arr_input);
    }


    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return CheckpointMakeDate::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return CheckpointMakeDate::where('id',$n_id)->decrement($s_field,$n_step);
    }

    /**
     * 字段自增-设置上限
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num_limit($n_id,$s_field = 'commend_num',$n_step = 1,$n_limit_num = 0)
    {
        if($n_limit_num == 0){
            $res = CheckpointMakeDate::where('id',$n_id)->increment($s_field,$n_step);
        }else{
            $res = CheckpointMakeDate::where('id',$n_id)->where($s_field,'<=',$n_limit_num - $n_step)->increment($s_field,$n_step);
        }
        return $res;
    }

    /**
     * 字段自增-设置上限
     *
     * @param int    $arr_where
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num_limit_by_where($arr_where,$s_field = 'commend_num',$n_step = 1,$n_limit_num = 0)
    {
        if($n_limit_num == 0){
            $res = CheckpointMakeDate::where($arr_where)->increment($s_field,$n_step);
        }else{
            $res = CheckpointMakeDate::where($arr_where)->where($s_field,'<=',$n_limit_num - $n_step)->increment($s_field,$n_step);
        }
        return $res;
    }

    /**
     * 字段自减-设置下限
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num_limit($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return CheckpointMakeDate::where('id',$n_id)->where($s_field,'>=',$n_step)->decrement($s_field,$n_step);
    }

    /**
     * 字段自减-设置下限
     *
     * @param array    $arr_where
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num_limit_by_where($arr_where,$s_field = 'commend_num',$n_step = 1)
    {
        return CheckpointMakeDate::where($arr_where)->where($s_field,'>=',$n_step)->decrement($s_field,$n_step);
    }

}